#include<my_header.h>

typedef struct Node_s
{
    int data;
    struct Node_s* next;
} Node_t;

typedef struct Queue_s
{
    int size;
    Node_t* front;
    Node_t* tail;
    
} Queue_t;

typedef struct Share_s{
    pthread_mutex_t mutex;
    pthread_cond_t sell_cond;
    pthread_cond_t produce_cond;
    Queue_t* que;
} Share_t;

int pop(Queue_t* que){ //头删
    if(que->size==0){
        printf("队列为空\n");
        return 0;
    }
    Node_t* tmp=que->front;
    if(que->size==1){
        que->front=NULL;
        que->tail=NULL;
    }else{
        que->front=que->front->next;
    }
    free(tmp);
    --que->size;
    return 1;
} 

int push(Queue_t* que,int data){
    Node_t* node=(Node_t*) calloc(1,sizeof(Node_t));
    node->data = data;
    if(que->size==0){
        que->front=node;
        que->tail=node;
        
    }else{
        que->tail->next=node;
        que->tail=node;
    }
    que->size++;
    return 1;
}

void print(Queue_t* que){
    Node_t* tmp=que->front;
    for(int i=0; i<que->size; i++){
        printf("%d ",tmp->data);
        tmp=tmp->next;
    }
    printf("\n");
}

void *seller(void* arg)
{   //消费者
    sleep(5);
    Share_t* share=(Share_t*) arg;
    while(1)
    {
        pthread_mutex_lock(&share->mutex);
        while (share->que->size==0)
        {
            pthread_cond_wait(&share->sell_cond,&share->mutex);
        }
        int data = share->que->front->data;
        pop(share->que);
        printf("消费了商品: %d, 当前队列大小: %d\n", data, share->que->size);
        pthread_cond_signal(&share->produce_cond);
        pthread_mutex_unlock(&share->mutex);
        sleep(1);
    }
    return NULL; 
}

void* producer(void* arg)
{
    Share_t* share=(Share_t*) arg;
    while(1){
        pthread_mutex_lock(&share->mutex);
        while(share->que->size==10){
            pthread_cond_wait(&share->produce_cond,&share->mutex);
        }
        int data=rand()%1000+1;
        push(share->que,data);
        printf("生产了商品: %d, 当前队列大小: %d\n", data, share->que->size);
        pthread_cond_signal(&share->sell_cond);
        pthread_mutex_unlock(&share->mutex);
        sleep(3);
    }
    return NULL;
}

int main() {
    Share_t* share = (Share_t*)calloc(1, sizeof(Share_t));
    share->que = (Queue_t*)calloc(1, sizeof(Queue_t));
    pthread_cond_init(&share->sell_cond, NULL);
    pthread_cond_init(&share->produce_cond, NULL);
    pthread_mutex_init(&share->mutex, NULL);

    srand(time(0));
    for (int i = 0; i < 8; i++) {
        Node_t* node = (Node_t*)calloc(1, sizeof(Node_t));
        node->data = rand() % 1000 + 1;
        node->next = NULL;
        if (share->que->size == 0) {
            share->que->front = node;
            share->que->tail = node;
        } else {
            share->que->tail->next = node;
            share->que->tail = node;
        }
        share->que->size++;
    }

    pthread_t pro_arr[3], cons_arr[2];
    for (int i = 0; i < 3; i++) {
        pthread_create(&pro_arr[i], NULL, producer, share);
    }
    for (int i = 0; i < 2; i++) {
        pthread_create(&cons_arr[i], NULL, seller, share);
    }

    sleep(60);
    
    for (int i = 0; i < 3; i++) {
        pthread_cancel(pro_arr[i]);
    }
    for (int i = 0; i < 2; i++) {
        pthread_cancel(cons_arr[i]);
    }

    for (int i = 0; i < 3; i++) {
        pthread_join(pro_arr[i], NULL);
    }
    for (int i = 0; i < 2; i++) {
        pthread_join(cons_arr[i], NULL);
    }

    pthread_cond_destroy(&share->sell_cond);
    pthread_cond_destroy(&share->produce_cond);
    pthread_mutex_destroy(&share->mutex);
    
    printf("队列中剩余商品：\n");
    while (share->que->size > 0) {
       int data = share->que->front->data;
       pop(share->que);
       printf("%d号商品已经被删除\n", data);
    }
    free(share->que);
    free(share);
    printf("资源回收完成\n");
    return 0;
}


// //test
// int main(){
//     Queue_t* que =(Queue_t* ) calloc(1,sizeof(Queue_t));
//     for(int i=0; i<10; i++){
//         push(que,i);
//     }
//     print(que);
// }
// }